package com.classeven.downhill.state;


import com.classeven.downhill.define.iGameDefines;
import com.classeven.downhill.element.ABackground;
import com.classeven.downhill.element.AScene;
import com.classeven.downhill.element.ASprite;
import com.classeven.downhill.engine.GameEngine;
import com.classeven.downhill.engine.Utils;

public class State_Loading {

	GameEngine engine;
	ABackground bg_Load;
	ASprite bar, load_rotate;
	AScene scene;
	
	private int rot, plus;
	private float bar_max, bar_plus; 
	
	public State_Loading(GameEngine engine)
	{
		this.engine = engine;
		bg_Load = new ABackground(engine, iGameDefines.SPRITE_LOAD, 0);
		bar = new ASprite(engine, iGameDefines.SPRITE_LOAD, 1);
		load_rotate = new ASprite(engine, iGameDefines.SPRITE_LOAD, 2);
		scene =new AScene(0);
	}
	
	public void init()
	{
		setPos();
		engine.SetGameScene(scene);
		engine.SceneAttach(scene, bg_Load);
		engine.SceneAttach(scene, bar);
		engine.SceneAttach(scene, load_rotate);
	}
	
	private void setPos()
	{
		bar.theSprite().setX(Utils.GetRatioWidth(20));
		bar.theSprite().setY(bar.GetY() - Utils.GetRatioWidth(20));
		bar.theSprite().setWidth(Utils.GetRatioWidth(10));
		bar_max = bg_Load.theSprite().getWidth() - Utils.GetRatioWidth(30);
	}

	public void update()
	{
		rot++;
		if(rot == 360)
			rot =0;
		
		load_rotate.theSprite().setRotation(rot);
		
		bar_plus = Utils.GetRatioWidth(plus);
		plus++;
		
		if(bar.theSprite().getWidth() <= bar_max)
		bar.theSprite().setWidth(bar_plus);
		else
		{
			engine.SetNextMessageState(engine.NextMessageState());
		}
	}
	
	public void exit()
	{
		scene.theScene().detachChildren();
	}
	
}
